package com.ly.demo.taach.jdbc.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ly.demo.taach.jdbc.dao.IDeptDao;

public class DeptDao implements IDeptDao {

	@Override
	public List<Map<String, Object>> selectAll() throws SQLException {
		String sql = "select * from dept";
		//  try () 中定义的对象, 会自动被关闭, 该对象必须实现了 Closeable  AutoCloseable 接口
		try (DBHelper dbh = new DBHelper()) {
			return dbh.selectListMap(sql);
		}
	}

	@Override
	public List<Map<String, Object>> select(String dname, String loc) throws SQLException {
		// 组合条件查询, 暂未实现
		String sql = "select * from dept where 1=1"; // where 1=1
		ArrayList<Object> params = new ArrayList<>(); // 参数集合 list
		if (dname != null && dname.trim().isEmpty() == false) {
			sql += " and dname like ?";
			params.add("%" + dname.trim() + "%");
		}
		if (loc != null && loc.trim().isEmpty() == false) {
			sql += " and loc like ?";
			params.add("%" + loc.trim() + "%");
		}
		try (DBHelper dbh = new DBHelper()) {
			return dbh.selectListMap(sql, params.toArray());
		}
	}

	@Override
	public int insert(String dname, String loc) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void update(int deptno, String dname, String loc) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteByDeptno(int deptno) throws SQLException {
		// TODO Auto-generated method stub

	}

}
